﻿using Abp.Application.Services.Dto;
using Abp.Dependency;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using LpbPrj.Client.Options;
using LpbPrj.Client.Patients;
using LpbPrj.Client.Patients.Dtos;
using LpbPrj.Client.Records;
using LpbPrj.Client.Results;
using LpbPrj.Client.Users;
using MyPrj.WPF.ReportInfoes;
using System;
using System.Threading.Tasks;

namespace LpbPrj.Client.DatabaseHelper
{
    public class DataCenter : ISingletonDependency
    {
        //2: 使用属性注入获得 logger
        public ILogger Logger { get; set; }
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        private readonly IUserService _userService;
        private readonly IOptionService _optionService;
        private readonly IPatientService _patientService;
        private readonly IRecordService _recordService;
        private readonly IResultService _resultService;
        private readonly IReportService _reportService;

        public DataCenter(IUnitOfWorkManager unitOfWorkManager, IUserService userService, IOptionService optionService,
            IPatientService patientService, IRecordService recordService, IResultService resultService, IReportService reportService)
        {
            //3: 如果没有提供Logger，就不能记录日志
            Logger = NullLogger.Instance;
            _unitOfWorkManager = unitOfWorkManager;

            _userService = userService;
            _optionService = optionService;
            _patientService = patientService;
            _recordService = recordService;
            _resultService = resultService;
            _reportService = reportService;
        }

        static DataCenter dataCenter = null;
        static AbpFactory abpFactory = new AbpFactory();
        public static DataCenter GetDataCenter()
        {
            if (dataCenter == null)
            {
                dataCenter = abpFactory._bootstrapper.IocManager.Resolve<DataCenter>();
            }

            return dataCenter;
        }

        public static void FreeDataCenter()
        {
            if (dataCenter != null)
            {
                abpFactory._bootstrapper.IocManager.Release(dataCenter);
            }
            abpFactory._bootstrapper.Dispose();
        }

        public async Task<PatientEditDto> GetForEdit(int? input)
        {
            return await _patientService.GetForEdit(new NullableIdDto { Id = input });
        }

        public async Task<PatientListDto> CreateOrUpdate(PatientEditDto input)
        {
            return await _patientService.CreateOrUpdate(input);
        }

    }
}
